﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BHXH.LinqRepository
{
    // : Repository<Phieu_DangBang>
    public class DanhMucPhieu_DangBangRepository : Repository<Phieu_DangBang>
    {
         
        public List<Phieu_DangBang> GetAllByPhieuId(Guid PhieuId)
        {
            var obj = (from ds in dataContext.Phieu_DangBangs
                       where ds.PhieuID == PhieuId
                       select ds).ToList();
            return obj;
        }
        public Phieu_DangBang GetByPhieuID_MaCau(Guid phieuID, int maCau)
        {
            return (from ds in dataContext.Phieu_DangBangs
                    where ds.PhieuID == phieuID && ds.MaCau == maCau
                    select ds).FirstOrDefault();
        }
        public Phieu_DangBang GetOneById(Guid Id)
        {
            return dataContext.Phieu_DangBangs.Where(c => c.ID == Id).FirstOrDefault();
        }
        public void InsertAll(List<Phieu_DangBang> source)
        {
            //AddAll(source);
            foreach (Phieu_DangBang item in source)
            {
                item.ID = Guid.NewGuid();
                //Add(item);
                dataContext.Phieu_DangBangs.InsertOnSubmit(item);
            }
            //dataContext.Phieu_DangBangs.InsertAllOnSubmit(source);            
        }
        public void DeleteAll(List<Phieu_DangBang> source)
        {
            dataContext.Phieu_DangBangs.DeleteAllOnSubmit(source);
        }

        public void DeleteAllByPhieuId(Guid phieuId)
        {
            var obj = dataContext.Phieu_DangBangs.Where(c => c.PhieuID == phieuId).ToList();
            dataContext.Phieu_DangBangs.DeleteAllOnSubmit(obj);
        }
        public void DeleteAllByPhieuIdMaCau(Guid phieuId, int MaCau)
        {
            var obj = dataContext.Phieu_DangBangs.Where(c => c.PhieuID == phieuId && c.MaCau == MaCau).ToList();
            dataContext.Phieu_DangBangs.DeleteAllOnSubmit(obj);
        }
        public void EditOne(Phieu_DangBang one)
        {
            //dataContext.Phieu_DangBangs.Attach(one, true);
           // dataContext.SubmitChanges();
           // Update(one);
         
        }


        public decimal GetNumberBySTT(Guid PhieuId, int MaCau, int CotThu)
        {
            var obj = dataContext.Phieu_DangBangs.Where(c => c.PhieuID == PhieuId && c.MaCau == MaCau).FirstOrDefault();
            if (obj != null)
            {
                if (CotThu == 1)
                    return obj.Number1.GetValueOrDefault();
                if (CotThu == 2)
                    return obj.Number2.GetValueOrDefault();
                if (CotThu == 3)
                    return obj.Number3.GetValueOrDefault();
                if (CotThu == 4)
                    return obj.Number4.GetValueOrDefault();
            }
            else
                return 0;
            return 0;
        }
    }
}
